package demo;

import java.util.Stack;


//最小栈
public class MinStack {
     private Stack<Integer> s;
     private Stack<Integer> minStack;
     public MinStack() {
          s = new Stack<>();
          minStack = new Stack<>();
     }
     //入栈
     public void push(int val) {
          s.push(val);
          if(minStack.empty()) {
               minStack.push(val);
          } else {
               int  peekV = minStack.peek();
               if(val <= peekV) {
                    minStack.push(val);
               }
          }
     }
     //出栈
     public void pop() {
          if(!s.empty()) {
               int popV = s.pop();
               int peekVMinS = minStack.peek();
               if(popV == peekVMinS) {
                    minStack.pop();
               }
          }
     }
     //获取栈顶元素
     public int top() {
          if(!s.empty()) {
               return s.peek();
          }
          return -1;
     }
     //获取最小栈的栈顶元素
     public int getMin() {
          if(!minStack.empty()) {
               return minStack.peek();
          }
          return -1;
     }
}
